package com.lfk.justweengine.utils.tool;

import com.lfk.justweengine.utils.tools.ImageHelper;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

public class ImageHelperOhosTest {

    private final String iconFilePath = "resources/rawfile/graphic/icon.png";

    @Test
    public void testHandleImageEffect() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = ImageHelper.handleImageEffect(iconPiexMap,1,100,50);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testHandleImagePixelsOldPhoto() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = ImageHelper.handleImagePixelsOldPhoto(iconPiexMap);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testHandleImagePixelsRelief() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = ImageHelper.handleImagePixelsRelief(iconPiexMap);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }



    private PixelMap getPixelMapFormFilePath(Context context, String filename) {
        PixelMap s_bitmap = null;
        RawFileEntry rawFileEntry = context.getResourceManager().getRawFileEntry(filename);
        try {
            Resource rs = rawFileEntry.openRawFile();
            int length = rs.available();
            byte[] buffer = new byte[length];
            rs.read(buffer, 0, length);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";

            ImageSource imageSourceNoOptions = ImageSource.create(buffer, srcOpts);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            s_bitmap = imageSourceNoOptions.createPixelmap(decodingOptions);
            imageSourceNoOptions.release();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s_bitmap;
    }
}
